package com.fcl.mylibrary.banner;

import android.content.Context;
import android.support.v4.view.PagerAdapter;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.fcl.mylibrary.ImageLoader.ImageLoader;
import com.fcl.mylibrary.R;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by fang on 2016/8/26.
 */
public class MyPagerAdapter<T> extends PagerAdapter {

    private Context mContext;
    /**
     * 配置项
     */
    private MyPagerConfig mMyPagerConfig;
    /**
     * 待展示的数据
     */
    private List<T> mShowDatas = new ArrayList<>();
    /**
     * 展示数据类型，默认是图片
     */
    private DataType mDataType = DataType.IMAGE;
    /**
     * 图片的来源，默认是网络url
     */
    private ImageSrc mImageSrc = ImageSrc.NET_URL;
    /**
     * 点击事件回调接口
     */
    private MyPageListener mPageListener;

    public MyPagerAdapter(Context context) {
        this.mContext = context;
    }

    public MyPagerAdapter(Context context, List<T> datas) {
        this.mContext = context;
        this.mShowDatas = datas;
    }

    public void setmShowDatas(List<T> mShowDatas) {
        this.mShowDatas = mShowDatas;
        notifyDataSetChanged();
    }

    @Override
    public boolean isViewFromObject(View view, Object object) {
        return view == object;
    }

    @Override
    public int getCount() {
        return mShowDatas.size();
    }

    @Override
    public Object instantiateItem(ViewGroup container, final int position) {
        Object result = null;
        result = getView(position);
        ((View) result).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mPageListener != null) {
                    mPageListener.onClick(position);
                }
            }
        });
        if (position % 2 == 0) {
            ((View) result).setTag(R.id.test, "clear");
        }

        container.addView((View) result);
        return result;

    }

    private View getView(int position) {
        View view = null;
        switch (mDataType) {
            case FRAGMENT:
                break;
            case IMAGE:
                view = new ImageView(mContext);
                switch (mImageSrc) {
                    case NET_URL:
                        ImageLoader.getInstance().loadImage((String)mShowDatas.get(position), (ImageView)view, true);
                        break;
                    case DRAWABLE_ID:
                        ((ImageView) view).setBackgroundResource((Integer) mShowDatas.get(position));
                        break;
                }
                break;
        }
        return view;
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        container.removeView((View) object);
    }

    /**
     * 根据返回值判断是否刷新该子项
     *
     * @param object
     * @return
     */
    @Override
    public int getItemPosition(Object object) {
        if (((View)object).getTag(R.id.test)  == "clear") {
            return POSITION_NONE;
        } else {
            return POSITION_UNCHANGED;
        }

    }

    public void initConfig(MyPagerConfig config) {
        this.mMyPagerConfig = config;
//        if (config.getShowDatas() != null) {
//            mShowDatas = config.getShowDatas();
//        }
        if (config.getDataType() != null) {
            mDataType = config.getDataType();
        }
        if (config.getImageSrcType() != null) {
            mImageSrc = config.getImageSrcType();
        }
        if (config.getMyPageListener() != null) {
            mPageListener = config.getMyPageListener();
        }
    }
}
